package com.yby6.common.utils;

import com.antherd.smcrypto.sm2.Keypair;
import com.antherd.smcrypto.sm2.Sm2;

/**
 * @author yang shuai
 * @date 2023/1/5
 */
public class SM2Util {

    private static final String privateKey = "d00996395ccee3bced80c4bed01bb1bcfdf3d452277640de1b993e84190e3b99";
    private static final String publicKey = "040ce3204ce50f09409c627ff86e99f0ad1855301da6906323061d5cd769269df1c5803e355bb7a8d5f1f6ad39684191deeb732c62facbd28dfac4cebc5de3127d";

    /**
     * 解密
     *
     * @param text
     * @return
     */
    public static String decrypt(String text) {
        return Sm2.doDecrypt(text, privateKey);
    }

    public static String encode(String password) {
        return Sm2.doEncrypt(password, publicKey);
    }

    public void generateKeyPairHex() {
        // 生成出来的密钥可无缝衔接 前端 js 加解密
        Keypair keypair = Sm2.generateKeyPairHex();
        String privateKey = keypair.getPrivateKey(); // 公钥
        String publicKey = keypair.getPublicKey(); // 私钥

        System.out.println(privateKey);
        System.out.println(publicKey);

        System.out.println("========================");
    }


    public void t2() {
        Keypair keypair = Sm2.generateKeyPairHex();
        String privateKey = keypair.getPrivateKey(); // 公钥
        String publicKey = keypair.getPublicKey(); // 私钥
        System.out.println(privateKey);
        System.out.println(publicKey);
        System.out.println("========================");

        // d00996395ccee3bced80c4bed01bb1bcfdf3d452277640de1b993e84190e3b99
        // 040ce3204ce50f09409c627ff86e99f0ad1855301da6906323061d5cd769269df1c5803e355bb7a8d5f1f6ad39684191deeb732c62facbd28dfac4cebc5de3127d


        // cipherMode 1 - C1C3C2，0 - C1C2C3，默认为1
        // 使用公钥加密
        final String doEncrypt = Sm2.doEncrypt("https://yby6.com,https://yangbuyi.top", publicKey);
        System.out.println("加密： \n" + doEncrypt);
        // 私钥解密
        final String s = Sm2.doDecrypt(doEncrypt, privateKey);
        System.out.println("解密：\n" + s);
    }

}
